package com.illumination.monitor.common.utils;


import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;

import com.github.heqiao2010.lunar.LunarCalendar;
import com.illumination.monitor.common.enums.ResultEnum;
import com.illumination.monitor.common.enums.TraditionalFestivalEnum;
import com.illumination.monitor.common.exception.BusinessException;

public class DateUtils extends DateFormatUtils{

	private static final String YYYY_MM_DD_FORMAT = "yyyy-MM-dd";


	public static Date getCurrentWeeStartDate() {
		DateTime now = DateTime.now();
		String strNow = now.toString(YYYY_MM_DD_FORMAT);
		now = DateTime.parse(strNow);
		return now.toDate();
	}

	public static Date getCurrentWeeEndDate() {
		DateTime now = DateTime.now();
		String strNow = now.toString(YYYY_MM_DD_FORMAT);
		now = DateTime.parse(strNow).plusDays(1).minusSeconds(1);
		return now.toDate();
	}

	public static Calendar lunarConvertCalendar(LunarCalendar lunar) {
		Calendar today = LunarCalendar.lunar2Solar(lunar.getLyear(), lunar.getLmonth(), lunar.getLdate(), lunar.isLeapMonth());
		return today;
	}


	public static Calendar traditionalFestival(TraditionalFestivalEnum Traditional) {
		DateTime now = DateTime.now();
		LunarCalendar lunarCalendar = new LunarCalendar();
		Calendar calendar = Calendar.getInstance();
		switch (Traditional.getCode()) {
		case 0:
			lunarCalendar.setLyear(now.getYear());
			lunarCalendar.setLmonth(1);
			lunarCalendar.setLdate(1);
			if((System.currentTimeMillis() - lunarConvertCalendar(lunarCalendar).getTime().getTime() + 1000*60*60*24*6) / (1000*60*60*24) > 0) lunarCalendar.setLyear(now.getYear() + 1);
			break;
		case 1:
			lunarCalendar.setLyear(now.getYear());
			lunarCalendar.setLmonth(5);
			lunarCalendar.setLdate(5);
			break;
		case 2:
			calendar.set(now.getYear(), 4 - 1, sweepingDay(now.getYear()));
			if((System.currentTimeMillis() - calendar.getTime().getTime()) / (1000*60*60*24) > 0) {
				calendar.set(now.getYear()+1, 4 - 1, sweepingDay(now.getYear() + 1));
			}
			return calendar;
		case 3:
			lunarCalendar.setLyear(now.getYear());
			lunarCalendar.setLmonth(8);
			lunarCalendar.setLdate(15);
			break;
		case 4:
			lunarCalendar.setLyear(now.getYear());
			lunarCalendar.setLmonth(9);
			lunarCalendar.setLdate(9);
			break;
		case 5:
			lunarCalendar.setLyear(now.getYear());
			lunarCalendar.setLmonth(8);
			lunarCalendar.setLdate(15);
			break;
		case 6:
			lunarCalendar.setLyear(now.getYear());
			lunarCalendar.setLmonth(7);
			lunarCalendar.setLdate(7);
			break;


		case 7:
			calendar.set(now.getYear(), 1-1, 1);
			if((System.currentTimeMillis() - calendar.getTime().getTime()) / (1000*60*60*24) > 0) calendar.add(Calendar.YEAR, 1);
			return calendar;
		case 8:
			calendar.set(now.getYear(), 5-1, 1);
			if((System.currentTimeMillis() - calendar.getTime().getTime() + 1000*60*60*24*4) / (1000*60*60*24) > 0) calendar.add(Calendar.YEAR, 1);
			return calendar;
		case 9:
			calendar.set(now.getYear(), 10-1, 1);
			if((System.currentTimeMillis() - calendar.getTime().getTime() + 1000*60*60*24*6) / (1000*60*60*24) > 0) calendar.add(Calendar.YEAR, 1);
			return calendar;
		default:
			throw new BusinessException(ResultEnum.PARAM_ERROR);
		}
		if((System.currentTimeMillis() - lunarConvertCalendar(lunarCalendar).getTime().getTime()) / (1000*60*60*24) > 0) lunarCalendar.setLyear(now.getYear() + 1);
		return lunarConvertCalendar(lunarCalendar);

	}

	private static int sweepingDay(int year) {
		if (year == 2232) return 4;
		if (year < 1700)	throw new RuntimeException("1700年以前暂时不支持");
		if (year >= 3100) throw new RuntimeException("3100年以后暂时不支持");
		double[] coefficient = { 5.15, 5.37, 5.59, 4.82, 5.02, 5.26, 5.48, 4.70, 4.92, 5.135, 5.36, 4.60, 4.81, 5.04,5.26 };
		int mod = year % 100;
		return (int) (mod * 0.2422 + coefficient[year / 100 - 17] - mod / 4);
	}

}
